package com.eastcom.providor.task;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.eastcom.bm.atom.Atom_AbnormalScanJob;
import com.eastcom.common.util.CharliDate;
import com.eastcom.providor.BMSetting;
import com.eastcom.providor.dao.impl.DataAbnormalDao;
import com.eastcom.providor.dao.impl.DataSetDao;
import com.eastcom.providor.dao.impl.ScanJobDao;

public class ScanSchedulerManager {

	public static Logger log = BMSetting.getBMlog();
	
	private static ScanSchedulerManager instance=null;
	
	private static String SIMPLE_SUFFIX="SimpleTrigger";
	
	private static String CRON_SUFFIX="CronTrigger";
	
	public static String applicationContextPath=null;
	
	private Scheduler scheduler ;
	
	private ScanSchedulerManager(){
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		
		try {
			scheduler=schedulerFactory.getScheduler();
			scheduler.start();
			initJobSchedule();
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static ScanSchedulerManager getInstance(){
		if(instance==null)
			instance=new ScanSchedulerManager();
		
		return instance;
	}
	
	private void initJobSchedule() throws Exception{
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(applicationContextPath);
		ScanJobDao scanJobDao=(ScanJobDao)context.getBean("scanJobDao");
		
		IntegerAbnormalScanJob.dataSetDao=(DataSetDao)context.getBean("dataSetDao");
		IntegerAbnormalScanJob.dataAbnormalDao=(DataAbnormalDao)context.getBean("dataAbnormalDao");
		List<Atom_AbnormalScanJob> abnormalScanJobs=scanJobDao.selectAllAtomAbnormalScanJob();
		if(abnormalScanJobs==null||abnormalScanJobs.isEmpty())
			return;
		
		for(Atom_AbnormalScanJob abnormalScanJob:abnormalScanJobs)
			scheduleScanJobSimple(abnormalScanJob);
		
		System.out.println(new Date()+" initial abnormalScanJob with size "+abnormalScanJobs.size()+" completed");
	}
	
	protected void schedule(JobDetail jobDetail,Trigger trigger) throws SchedulerException{
		scheduler.scheduleJob(jobDetail, trigger);
	}
	
	protected boolean pauseTrigger(String triggerName, String group) {   
		try {
		    scheduler.pauseTrigger(triggerName, group);
		    return true;
		}
		catch (SchedulerException e) {
		    e.printStackTrace();
		    log.error("pauseTrigger "+triggerName+" "+group+" error", e);
		}
		
		return false;
	}

	protected boolean removeTrigger(String triggerName, String group) {   
		try {
		     scheduler.pauseTrigger(triggerName, group);
		     return scheduler.unscheduleJob(triggerName, group);
		} 
		catch (SchedulerException e) {   
			e.printStackTrace();
		    log.error("removeTrigdger "+triggerName+" "+group+" error", e);
		}
		
		return false;
	}
	
	public void scheduleScanJobSimple(Atom_AbnormalScanJob abnormalScanJob) throws SchedulerException{
		if(abnormalScanJob==null||abnormalScanJob.getDataSetId()==null)
			return;
		
		Integer repeatNum=abnormalScanJob.getRepeatNum()==null?SimpleTrigger.REPEAT_INDEFINITELY:abnormalScanJob.getRepeatNum();
		JobDetail jobDetail=new JobDetail(abnormalScanJob.getDataSetId(),abnormalScanJob.getDataSetId(), IntegerAbnormalScanJob.class);
		//JobDetail jobDetail=new JobDetail(abnormalScanJob.getDataSetId(),abnormalScanJob.getDataSetId(), TestJob.class);
		
		Map map = jobDetail.getJobDataMap();   
		map.put("dataSetId", abnormalScanJob.getDataSetId());
		map.put("backScanHour", abnormalScanJob.getBackScanHour());
		
		Trigger trigger=new SimpleTrigger(abnormalScanJob.getDataSetId()+SIMPLE_SUFFIX,abnormalScanJob.getDataSetId(), abnormalScanJob.getFirstTime(), null,-1, abnormalScanJob.getInterval()*1000);
		scheduler.scheduleJob(jobDetail, trigger);
	}
	
	public void scheduleScanJobCron(Atom_AbnormalScanJob abnormalScanJob){
		
	}
	
	public boolean unscheduleScanJob(String dataSetId){
		return removeTrigger(dataSetId+SIMPLE_SUFFIX,dataSetId);
	}
	
	public void shutDown(){
		if(scheduler!=null)
			try {
				scheduler.shutdown();
				System.out.println(new Date()+" ScanSchedulerManager shutDown completed");
			} 
		    catch (SchedulerException e) {
				e.printStackTrace();
				log.error("ScanSchedulerManager shutDown error",e);
			}
	}
	

	
}
